//## ID 중복 체크 ####################
function fnDupIdCheck()
{
	if (!$("#userId").val())
	{
		alert("사용자 ID를 바르게 입력해 주세요!");
		$("#userId").focus();
		return;
	}
	var valUrl = "/action/member/duplicateIdCheck.asp";
	var param  = "userId="+encodeURIComponent($("#userId").val());
	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());

			if ($(xml).find("MSG").text() > 0)
			{
				alert("이미 사용중인 아이디 입니다.");
				$("#userIdCheck").val("D");
			}
			else if ($(xml).find("MSG").text() == 0)
			{
				alert("사용 가능한 아이디 입니다.");
				$("#userIdCheck").val("OK");
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 사원정보 정보 저장 ####################
function fnSave()
{
	if (!$("#userId").val())
	{
		alert("사용자 ID를 바르게 입력해 주세요!");
		$("#userId").focus();
		return;
	}
	if (!$("#userIdCheck").val())
	{
		alert("사용자 ID중복 체크를 해주세요!");
		$("#userId").focus();
		return;
	}
	if ($("#userIdCheck").val() == "D")
	{
		alert("사용자 ID를 이미 사용하고 있습니다. 다른 아이디를 입력해 주세요.");
		$("#userIdCheck").val("");
		$("#userId").val("");
		$("#userId").focus();
		return;
	}
	if (!$("#userNm").val())
	{
		alert("이름을 바르게 입력해 주세요!");
		$("#userNm").focus();
		return;
	}
	if (!$("#passwd").val())
	{
		alert("비밀번호 바르게 입력해 주세요!");
		$("#passwd").focus();
		return;
	}

	var param  = "userId="+encodeURIComponent($("#userId").val());
		param += "&userNm="+encodeURIComponent($("#userNm").val());
		param += "&passwd="+$("#passwd").val();
		param += "&apprPass="+$("#apprPass").val();
		param += "&uDuty="+$("#uDuty").val();
		param += "&uPart="+encodeURIComponent($("#uPart").val());
		param += "&uPhone="+encodeURIComponent($("#uPhone").val());
		param += "&compCd="+$("#compCd").val();
		param += "&uEmail="+encodeURIComponent($("#uEmail").val());
		param += "&uRemark="+encodeURIComponent($("#uRemark").val());
		param += "&uLevel="+$("#uLevel").val();
		param += "&gwYN="+$("#gwYN").val();
		param += "&delYN="+$("#delYN").val();
		var perms = "";
		for (k=1; k<10; k++)
		{
			if ($("#p"+k+"0").is(":checked"))
			{
				perms += "/"+k+"0/";
			}
			if ($("#p"+k+"1").is(":checked"))
			{
				perms += "/"+k+"1/";
			}
			if ($("#p"+k+"2").is(":checked"))
			{
				perms += "/"+k+"2/";
			}
			if ($("#p"+k+"3").is(":checked"))
			{
				perms += "/"+k+"3/";
			}

		}
		param += "&uPerm="+perms;

	$.ajax({
		url: '/action/member/save.asp',
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 사원정보 정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").show();
	setNullRow("loadSpan","1");
	var valUrl = "/action/member/list.asp";
	var param = "findUserNm="+encodeURIComponent($("#findUserNm").val());

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("Members").find("MemList").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var userId			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("userId").text();
		var userNm			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("userNm").text();
		var uPhone			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("uPhone").text();
		var uLevel			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("uLevel").text();
		var level			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("level").text();
		var duty			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("duty").text();
		var gwYN			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("gwYN").text();
		var delYN			= $(G_grid01Rslt).find("Members").find("MemList").eq(j).find("delYN").text();

		ji = j+1;
		
		var showFlag = "";
		if(delYN == "N") showFlag = "사용중";
		else showFlag = "정 지";
		
		if (delYN == "Y") var bgColor = "#F2F2F2";
		else var bgColor = "#FFFFFF";
		//var level = "";
		//if (uLevel == "10") level = "일반";
		//else if (uLevel == "50") level = "관리자";
		//else if (uLevel == "05") level = "외부고객";
		
		var gwUser = "&nbsp;";
		if (gwYN == 'Y') gwUser = "사용";
		
		grid01Array[0] = ji;
		grid01Array[1] = userId;
		grid01Array[2] = userNm;
		grid01Array[3] = duty;
		grid01Array[4] = uPhone+"&nbsp;";
		grid01Array[5] = level;
		grid01Array[6] = showFlag;
		grid01Array[7] = gwUser;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,,,,,,,", "fnSetMember('"+userId+"')", boldLine);
	}
}


//## 사원정보 상세정보 표시 ####################
function fnSetMember(userId)
{
	var valUrl = "/action/member/view.asp";
	var param = "userId="+userId;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();
			$("#userIdCheck").val("OK");

			$("#userId").val($(xml).find("userId").text());
			$("#passwd").val($(xml).find("passwd").text());
			$("#userNm").val($(xml).find("userNm").text());
			$("#apprPass").val($(xml).find("apprPass").text());
			$("#uDuty").val($(xml).find("uDuty").text());
			$("#uLevel").val($(xml).find("uLevel").text());
			$("#uPart").val($(xml).find("uPart").text());
			$("#uEmail").val($(xml).find("uEmail").text());
			$("#uPhone").val($(xml).find("uPhone").text());
			$("#compCd").val($(xml).find("compCd").text());
			$("#company").val($(xml).find("company").text());
			$("#uRemark").val($(xml).find("uRemark").text());
			$("#gwYN").val($(xml).find("gwYN").text());
			$("#delYN").val($(xml).find("delYN").text());
			$("#accessInfo").val("최종수정 : "+$(xml).find("modDt").text());
			
			for (k=1; k<10; k++)
			{
				if ($(xml).find("uPerm").text().indexOf("/"+k+"0/") >-1)
				{
					$("#p"+k+"0").attr("checked",true);
				}				
				if ($(xml).find("uPerm").text().indexOf("/"+k+"1/") >-1)
				{
					$("#p"+k+"1").attr("checked",true);
				}				
				if ($(xml).find("uPerm").text().indexOf("/"+k+"2/") >-1)
				{
					$("#p"+k+"2").attr("checked",true);
				}					
				if ($(xml).find("uPerm").text().indexOf("/"+k+"3/") >-1)
				{
					$("#p"+k+"3").attr("checked",true);
				}				
			}
			if ($(xml).find("compCd").text()) fnFindCompany($(xml).find("company").text(),$(xml).find("compCd").text(),'compCd');
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 사원정보 정보 Reset 기능
function fnReset()
{
	$("#userId").val("");
	$("#userNm").val("");
	$("#apprPass").val("");
	$("#uDuty").val("");
	$("#passwd").val("");
	$("#uPart").val("");
	$("#uPhone").val("");
	$("#uEmail").val("");
	$("#uRemark").val("");
	$("#uLevel").val("");
	$("#gwYN").val("Y");
	$("#delYN").val("N");
	$("#compCd").val("");
	$("#company").val("");
	$("#userIdCheck").val("");
	$("#accessInfo").val("");
	for (k=1; k<10; k++)
	{
		$("#p"+k+"0").attr("checked",false);
		$("#p"+k+"1").attr("checked",false);
		$("#p"+k+"2").attr("checked",false);
		$("#p"+k+"3").attr("checked",false);
	}
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findUserNm").val("");
}